﻿using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace TestGenerator
{
    class TestParser
    {
        private string pathToFile;

        public TestParser(string fullPathToFile)
        {
            this.pathToFile = fullPathToFile;
        }

        public string PathToFile
        {
            get
            {
                return this.pathToFile;
            }
        }

        public List<QuestionSet> Parse()
        {
            List<QuestionSet> listOfQuestions = new List<QuestionSet>();
            Regex regQuestion = new Regex(@"<p class=Question>Question. (?<question>(.|\s)*?)</p>");
            Regex regAnswersCorrect = new Regex(@"<p class=CorrectAnswer>Correct. (?<correctAnswer>(.|\s)*?)</p>");
            Regex regAnswersIncorrect = new Regex(@"<p class=WrongAnswer>Wrong. (?<incorrectAnswer>(.|\s)*?)</p>");
            List<string> questionsInStrings = this.ParseFileToListOfStrings();
            for (int i = 0; i < questionsInStrings.Count; i++)
            {
                Match match = regQuestion.Match(questionsInStrings[i]);
                QuestionSet q = new QuestionSet();
                q.Question = match.Groups["question"].ToString();
                MatchCollection correctAnswers = regAnswersCorrect.Matches(questionsInStrings[i]);
                foreach (Match matchC in correctAnswers)
                {
                    q.AddAnswer(matchC.Groups["correctAnswer"].ToString(), true);
                }
                MatchCollection incorrectAnswers = regAnswersIncorrect.Matches(questionsInStrings[i]);
                foreach (Match matchI in incorrectAnswers)
                {
                    q.AddAnswer(matchI.Groups["incorrectAnswer"].ToString(), false);
                }
                listOfQuestions.Add(q);
            }
            return listOfQuestions;
        }

        private List<string> ParseFileToListOfStrings()
        {
            const string QUESTION_OPEN_TAG = "<p class=Question>";
            List<string> result = new List<string>();
            StreamReader reader = new StreamReader(this.pathToFile, Encoding.Default);
            string entireFileToString = null;
            using (reader)
            {
                entireFileToString = reader.ReadToEnd();
            }
            int currentPos = -1;
            bool empty = true;
            while (true)
            {
                currentPos = entireFileToString.IndexOf(QUESTION_OPEN_TAG, currentPos + 1);
                string toAdd = null;
                if (entireFileToString.IndexOf(QUESTION_OPEN_TAG, currentPos + 1) < 0)
                {
                    if (empty)
                    {
                        break;
                    }
                    else
                    {

                        toAdd = entireFileToString.Substring(currentPos, entireFileToString.Length - currentPos);
                        result.Add(toAdd);
                        break;
                    }
                }
                int nextPos = entireFileToString.IndexOf(QUESTION_OPEN_TAG, currentPos + 1);
                toAdd = entireFileToString.Substring(currentPos, nextPos - currentPos);
                result.Add(toAdd);
                empty = false;
            }
            return result;
        }

    }
}